from utils.db import db


class Service:
    def __init__(self):
        self.id = None
        self.name = ""
        self.price = 0.0
        self.duration = 0
        self.description = ""

    @staticmethod
    def get_all():
        """获取所有服务项目"""
        sql = "SELECT * FROM services ORDER BY id"
        return db.fetch_all(sql)

    @staticmethod
    def get_by_id(service_id):
        """根据ID获取服务项目"""
        sql = "SELECT * FROM services WHERE id = ?"
        return db.fetch_one(sql, (service_id,))

    def save(self):
        """保存或更新服务项目"""
        if self.id:
            sql = """
            UPDATE services 
            SET name=?, price=?, duration=?, description=?
            WHERE id=?
            """
            params = (self.name, self.price, self.duration,
                      self.description, self.id)
        else:
            sql = """
            INSERT INTO services (name, price, duration, description)
            VALUES (?, ?, ?, ?)
            """
            params = (self.name, self.price, self.duration, self.description)

        db.execute(sql, params)
        return True

    @staticmethod
    def delete(service_id):
        """删除服务项目"""
        sql = "DELETE FROM services WHERE id = ?"
        db.execute(sql, (service_id,))
        return True
